package com.xmtlxx.tms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.tms.domain.bo.AddressBo;
import com.xmtlxx.tms.domain.vo.AddressVo;
import com.xmtlxx.tms.domain.vo.CarrierVo;
import com.xmtlxx.tms.service.IAddressService;
import com.xmtlxx.tms.service.ICarrierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 地址Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "地址控制器", tags = {"地址管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/tms/address")
public class AddressController extends BaseController {

    private final IAddressService iAddressService;

    private final ICarrierService iCarrierService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<AddressVo> initBean(){
        AddressVo vo = new AddressVo();
        if(LoginHelper.getLoginUser().getRolePermission().contains("smscys")){
            vo.setIsShow(false);
            CarrierVo carrierVo = iCarrierService.selectCarrierByDeptId(LoginHelper.getDeptId());
            vo.setCarrierId(carrierVo.getId());
        }else {
            vo.setIsShow(true);
        }
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询地址列表
     */
    @ApiOperation("查询地址列表")
    @SaCheckPermission("tms:address:list")
    @GetMapping("/list")
    public TableDataInfo<AddressVo> list(@Validated(QueryGroup.class) AddressBo bo, PageQuery pageQuery) {
        return iAddressService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出地址列表
     */
    @ApiOperation("导出地址列表")
    @SaCheckPermission("tms:address:export")
    @Log(title = "地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated AddressBo bo, HttpServletResponse response) {
        List<AddressVo> list = iAddressService.queryList(bo);
        ExcelUtil.exportExcel(list, "地址", AddressVo.class, response);
    }

    /**
     * 获取地址详细信息
     */
    @ApiOperation("获取地址详细信息")
    @SaCheckPermission("tms:address:query")
    @GetMapping("/{id}")
    public R<AddressVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        AddressVo vo = iAddressService.queryById(id);
        if(LoginHelper.getLoginUser().getRolePermission().contains("smscys")){
            vo.setIsShow(false);
        }else {
            vo.setIsShow(true);
        }
        return R.ok(vo);
    }

    /**
     * 新增地址
     */
    @ApiOperation("新增地址")
    @SaCheckPermission("tms:address:add")
    @Log(title = "地址", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AddressVo vo) {
        AddressBo bo = new AddressBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iAddressService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改地址
     */
    @ApiOperation("修改地址")
    @SaCheckPermission("tms:address:edit")
    @Log(title = "地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AddressVo vo) {
        AddressBo bo = new AddressBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iAddressService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除地址
     */
    @ApiOperation("删除地址")
    @SaCheckPermission("tms:address:remove")
    @Log(title = "地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iAddressService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
